package com.hkccommunity.service.impl;

import com.hkccommunity.entity.AnswerParent;
import com.hkccommunity.mapper.AnswerParentMapper;
import com.hkccommunity.service.AnswerParentService;
import com.hkccommunity.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import java.util.Map;

public class AnswerParentServiceImpl implements AnswerParentService {

    private SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();

    @Override
    public int getCountByProblem(Long problemId) {
        SqlSession sqlSession = factory.openSession();
        AnswerParentMapper mapper = sqlSession.getMapper(AnswerParentMapper.class);
        int count = mapper.getCountByProblem(problemId);
        sqlSession.close();
        return count;
    }

    @Override
    public List<AnswerParent> getAnswerByProblemId(Long problemId) {
        SqlSession sqlSession = factory.openSession();
        AnswerParentMapper mapper = sqlSession.getMapper(AnswerParentMapper.class);
        List<AnswerParent> answerByProblemId = mapper.getAnswerByProblemId(problemId);
        sqlSession.close();
        return answerByProblemId;
    }

    @Override
    public int addAnswerParent(AnswerParent parent) {
        SqlSession sqlSession = factory.openSession();
        AnswerParentMapper mapper = sqlSession.getMapper(AnswerParentMapper.class);
        int i = mapper.addAnswerParent(parent);
        sqlSession.commit();
        sqlSession.close();

        return i;
    }
}
